package com.changgou.order.service.impl;

import com.changgou.order.dao.TaskHisMapper;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import com.changgou.order.pojo.TaskHis;
import com.changgou.order.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskHisMapper taskHisMapper;

    @Override
    public void DelTask(Task task) {
        //设置任务数据的删除时间
        task.setDeleteTime(new Date());
        Long id = task.getId();
        //用于Bean拷贝,因为TaskHis表中的主键为自增长
        task.setId(null);

        //将任务数据复制到历史任务对象,然后存储到历史任务数据表中
        TaskHis taskHis = new TaskHis();
        BeanUtils.copyProperties(task, taskHis);
        taskHisMapper.insertSelective(taskHis);

        //删除原任务数据
        task.setId(id);
        taskMapper.deleteByPrimaryKey(task);

        System.out.println("订单服务完成了添加历史任务并删除原有任务数据");
    }
}
